package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Company;
import com.beans.MsgReply;
import com.beans.Position;
import com.beans.User;
import com.beans.UserLog;
import com.service.UserManager;
import com.vo.Page;

public class UserController extends UniversalController {
	private UserManager userManager;

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public ModelAndView getViewListUserByRoller(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("user");
	}

	public ModelAndView getViewLogout(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("logout");
	}

	public ModelAndView getViewPwd(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("pwd");
	}

	public ModelAndView getListUserByRoller(HttpServletRequest request,
			HttpServletResponse response) {
		String rollerCode = request.getParameter("rollerCode");
		// getMessage(response,
		// getJsonFromJSONArray(userManager.getListUser(rollerCode)));
		return null;
	}
	//登陆
	public ModelAndView userHome(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		User u=(User)request.getSession().getAttribute("user");
		if(u==null)return new ModelAndView("user/loginError");
		map.put("o", u);
		return new ModelAndView("user/Home", map);
	}
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		String loginName = request.getParameter("loginName");
		String pwd = request.getParameter("pwd");
		String code = (String) request.getParameter("code");
		String validateCode = (String)request.getSession().getAttribute("validateCode");
		String ip=request.getLocalAddr();
		System.out.println(ip);
		if(validateCode==null){
				map.put("loginName", loginName);
				map.put("codeError", "验证码过期！");
				return new ModelAndView("user/loginError", map);
		}
		if (null == code||"".equals(code) || !code.toUpperCase().equals(validateCode.toUpperCase())) 
			{
				map.put("loginName", loginName);
				map.put("codeError", "验证码错误！");
				return new ModelAndView("user/loginError", map);
			}
		User u = userManager.getUserDao().login(loginName, pwd,request.getRemoteAddr());
		if (u == null) {
			map.put("loginName", loginName);
			return new ModelAndView("user/loginError", map);
		}
		request.getSession().setAttribute("user", u);
		map.put("o", u);
		return new ModelAndView("Home", map);
	}
	public ModelAndView loginout(HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return new ModelAndView("user/loginout");
	}
	public ModelAndView reg(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		String msg=null;
		try {
			//Object obj = this.getObjectFromJson(request, response, this.obj);
			User u=new User();
			bind(request,u);
			u.setEnabled("1");
			u.setIp(request.getLocalAddr());
			getUniversalManager().insert(u);
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			getMessage(response, "{\"success\":false,\"err\":\""+e.getMessage()+"\"}");
		}
		return null;
	}

	public ModelAndView queryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User user=(User)getObjectFromJson(request, response, this.obj);
		Page page=userManager.getUserDao().list(this.getPageNo(request), pageSize, user);
		result.put("o", page);
		return new ModelAndView("user/queryUserList", result);
	}
	public ModelAndView topUserList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User u=new User();
		String type=request.getParameter("type");
		String top=request.getParameter("top");
		int int_top=Integer.parseInt(top);
		List list=null;
		if("new".equals(type))list=userManager.getUserDao().topNewUser(int_top);
		else if("view".equals(type))list=userManager.getUserDao().topViewUser(int_top);
		else list=userManager.getUserDao().topFixUser(int_top);
		result.put("o", list);
		return new ModelAndView("user/topUserList", result);
	}
	public ModelAndView showUser(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		String id=request.getParameter("id");
		result.put("o", userManager.getUserDao().getUser(Integer.parseInt(id)));
		return new ModelAndView("user/showUser", result);
	}
	//修改界面
	public ModelAndView modifyUser(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		User u=(User)request.getSession().getAttribute("user");
		result.put("o", u);
		return new ModelAndView("user/modifyUser", result);
	}
	//更新数据
	public ModelAndView updateUser(HttpServletRequest request,
			HttpServletResponse response) {
		User u=(User)request.getSession().getAttribute("user");
		User u2=null;
		String msg="";
		try {
			 u2=(User)this.getObjectFromJson(request, response, this.obj);
			 System.out.println(u2);
			 u.setRealName(u2.getRealName());
			 u.setAddress(u2.getAddress());
			 u.setAge(u2.getAge());
			 u.setDegree(u2.getDegree());
			 u.setEmail(u2.getEmail());
			 u.setHeight(u2.getHeight());
			 u.setMajor(u2.getMajor());
//			 u.setMarried(u2.getMarried());
			 u.setMarried(u2.getMarried());
			 u.setNation(u2.getNation());
			 u.setPhone(u2.getPhone());
			 u.setPic(u2.getPic());
			 u.setPosition(u2.getPosition());
			 u.setQq(u2.getQq());
			 u.setRegion_treeCode(u2.getRegion_treeCode());
			 u.setSchool(u2.getSchool());
			 u.setSex(u2.getSex());
			 u.setSummary(u2.getSummary());
			 u.setTel(u2.getTel());
			 u.setWages(u2.getWages());
			 u.setWorkType(u2.getWorkType());
			 u.setWorkYear(u2.getWorkYear());
			 userManager.getUserDao().update(u);
			 msg="操作成功！";
		} catch (Exception e) {
			 msg="操作失败！";
			e.printStackTrace();
		}
		getMessage(response, msg);
		return null;
	}
	
	public ModelAndView querypositionByUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		Position p=new Position();
		bind(request,p);
		Page page = getUserManager().getPositionDao().queryList(this.getPageNo(request), pageSize, p);
		result.put("o", page);
		return new ModelAndView("user/positionlist", result);
	}
	//我关注的职位
	public ModelAndView myFocusPosition(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		Position p=new Position();
		User u=(User)request.getSession().getAttribute("user");
		bind(request,p);
		Page page = getUserManager().getUserDao().myFocusPosition(this.getPageNo(request), pageSize, u.getId(),p);
		result.put("o", page);
		return new ModelAndView("user/myFocusPosition", result);
	}
	//我关注的企业
	public ModelAndView myFocusCompany(HttpServletRequest request,HttpServletResponse response) throws Exception{
		User u=(User)request.getSession().getAttribute("user");
		Map result=new HashMap();
		Company c=new Company();
		bind(request,c);
		Page page=userManager.getUserDao().myFocusCompany(this.getPageNo(request), pageSize,u.getId(), c);
		result.put("o", page);
		return new ModelAndView("user/myFocusCompany",result);
	}
	public ModelAndView getCheckUserLoginName(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		map.put("loginName", request.getParameter("loginName"));
		Page page=(Page)getUniversalManager().findPageByCriteria(1, 1, map, User.class);
		if(page.getDataList().size()==0)
			getMessage(response, "恭喜你，可以使用！");
		else
			getMessage(response, "很抱歉，不能使用！");
		return null;
	}
	public ModelAndView myPwd(HttpServletRequest request,
			HttpServletResponse response) {
		User u=(User)request.getSession().getAttribute("user");
		String oldpwd=request.getParameter("oldPwd");
		if(oldpwd==null)return new ModelAndView("user/myPwd");
		String pwd1=request.getParameter("pwd1");
		if(getUserManager().getUserDao().myPwd(u.getId(), oldpwd, pwd1))
			getMessage(response, "操作成功！");
		else
			getMessage(response, "操作失败！");
		return null;
	}
	public ModelAndView userMsgReplyList(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Map param = new HashMap();
		User u=(User)request.getSession().getAttribute("user");
		String pid = request.getParameter("pid");
		try {
			param.put("tableName", "j_user");
			param.put("pid", u.getId());
			Page page = getUniversalManager().findPageByCriteria(
					this.getPageNo(request), pageSize, param, MsgReply.class);
			result.put("o", page);
			result.put("userID", u.getId());
			return new ModelAndView("user/msgReplyList", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public ModelAndView getFixUser(HttpServletRequest request,
			HttpServletResponse response){
		String id=request.getParameter("id");
		String fix=request.getParameter("fix");
		try{
			if(fix==null||"".equals(fix))
				getUserManager().getUserDao().fix(Integer.parseInt(id), 1);
			else
				getUserManager().getUserDao().fix(Integer.parseInt(id), Integer.parseInt(fix));
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		}catch(Exception e){
			getMessage(response, "{\"success\":false,\"err\":\""+e.getMessage()+"\"}");
		}
		return null;
	}
}
